package com.zerdoor.controller;

import com.zerdoor.model.UserInfo;
import com.zerdoor.pagehelper.PageHelper;
import com.zerdoor.pagehelper.PageInfo;
import com.zerdoor.service.RaceService;
import com.zerdoor.service.UserCenterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 竞赛活动
 * Created by xiaoji on 2015/1/22.
 */
@Controller
@RequestMapping("race")
public class RaceController {
    @Autowired
    private RaceService raceService;

    @Autowired
    private UserCenterService userCenterService;

    @RequestMapping(value = "/index",method = RequestMethod.GET)
    public String index(){return "/race/view";}

    /**
     * 获取竞赛活动数据
     * @param request
     * @param page
     * @param rows
     * @return
     */
    @RequestMapping(value = "/view",method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> view(HttpServletRequest request,int page,int rows){
        Map<String, Object> result = new HashMap<String, Object>();
        PageHelper.startPage(page, rows);
        result.put("title",request.getParameter("title"));
        result.put("progress",request.getParameter("progress"));
        List<Map<String, Object>> list = raceService.getRaceInfo(result);
        PageInfo pageInfo = new PageInfo(list);
        result.put("rows", list);
        result.put("total", pageInfo.getTotal());
        return result;
    }

    /**
     * 通过审核
     * @return
     */
    @RequestMapping(value = "/examine",method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> examine(int id,int userId,String title,HttpServletRequest request){
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            raceService.updateRaceProgress(id);

            UserInfo userInfo = (UserInfo)request.getSession().getAttribute("curManager");
            Map<String,Object> msg = new HashMap<String,Object>();

            msg.put("sender",userInfo.getUserId());
            msg.put("receiver",userId);
            msg.put("theme",title + " 审核通过");
            msg.put("content","您发布的竞赛[" + title +"]审核通过了！");
            userCenterService.addUserMessage(msg);
        } catch (Exception e) {
            result.put("errormsg","审核通过失败");
        }
        return result;
    }

    /**
     * 推荐
     * @return
     */
    @RequestMapping(value = "/recommend",method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> recommend(int id){
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            raceService.updateRaceRecom(id);
        } catch (Exception e) {
            result.put("errormsg","推荐失败");
        }
        return result;
    }

    /**
     * 推荐
     * @return
     */
    @RequestMapping(value = "/cancurRecommend",method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> cancurRecommend(int id){
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            raceService.updateRaceCancelRecom(id);
        } catch (Exception e) {
            result.put("errormsg","推荐失败");
        }
        return result;
    }
}
